package net.contrend.system.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import net.contrend.util.SqlUtils;
import net.contrend.util.StringUtils;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class UserRoleDao extends BaseDao {
    
    private String table = "ct_userrole";
    
    public UserRoleDao() {
    }
    
    public UserRoleDao(JdbcTemplate jt) {
        this.jt = jt;
    }
    
    public int[] insert(String userId, String[] roleIds) {
        if (StringUtils.isNotBlank(userId) && null != roleIds) {
            this.deleteByUserId(userId);
            int[] ints = new int[roleIds.length];
            for (int i = 0; i < roleIds.length; i++) {
                if (StringUtils.isNotBlank(roleIds[i])) {
                    ints[i] = this.insert(userId, roleIds[i]);
                }
            }
            return ints;
        }
        return null;
    }
    
    public int insert(String userId, String roleId) {
        String columns = "userid, roleid";
        String sql = SqlUtils.getInsertSql(this.table, columns);
        System.out.println(String.format("\n%1$s\n", sql));
        
        return this.jt.update(sql,
                new Object[]{
                    userId,
                    roleId
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR
                });
    }
    
    public int deleteByUserId(String userId) {
        String sql = SqlUtils.getDeleteSql(this.table, "userid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{userId}, new int[]{Types.VARCHAR});
    }
    
    public int deleteByRoleId(String roleId) {
        String sql = SqlUtils.getDeleteSql(this.table, "roleid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{roleId}, new int[]{Types.VARCHAR});
    }
    
    public int delete(String userId, String roleId) {
        String sql = SqlUtils.getDeleteSql(this.table, "userid = ? and roleid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{userId, roleId}, new int[]{Types.VARCHAR, Types.VARCHAR});
    }
    
    public List<String> getRoleIds(String userId) {
        return this.getRoleIds(userId, true);
    }
    
    @SuppressWarnings("unchecked")
    public List<String> getRoleIds(String userId, boolean isValid) {
        String sql = "" +
        		"select ur.roleid from ct_userrole ur " +
        		"    left join ct_role r on r.roleid = ur.roleid " +
        		"where userid = ? %1$s";
        sql = String.format(sql, isValid ? "and r.status = '1'" : "");
        System.out.println(String.format("\n%1$s\n", sql));
        List<String> roleIds = this.jt.query(sql, new Object[]{userId}, new RowMapper() {

            @Override
            public Object mapRow(ResultSet rs, int index) throws SQLException {
                return rs.getString("roleid");
            }
            
        });
        return roleIds;
    }
    
}
